﻿namespace PublicModels.Draft;

/// <summary>
/// 地图标绘
/// </summary>
public class Draft
{
    /// <summary>
    /// 地图标绘ID
    /// </summary>
    [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
    public int ID { get; set; }

    /// <summary>
    /// 地图标绘拥有者的ID
    /// </summary>
    public int OwnerID { get; set; }

    /// <summary>
    /// 中心点经度
    /// </summary>
    public double Lng { get; set; }

    /// <summary>
    /// 中心点纬度
    /// </summary>
    public double Lat { get; set; }

    /// <summary>
    /// 缩放级别
    /// </summary>
    public byte Zoom { get; set; }

    private string name = string.Empty;
    /// <summary>
    /// 标绘名称
    /// </summary>
    public string Name
    {
        get { return name; }
        set { name = value.Trim(); }
    }

    /// <summary>
    /// 创建日期
    /// </summary>
    public DateTime? CreateTime { get; set; } = null;

    /// <summary>
    /// 中心点
    /// </summary>
    [SugarColumn(IsIgnore = true)]
    public Point Center => new(Lng, Lat);

    /// <summary>
    /// 所有地图注记列表
    /// </summary>
    [SugarColumn(IsIgnore = true)]
    public List<Geometry> MapAnnotations
    {
        get
        {
            var temp = new List<Geometry>(Points.Count + LineStrings.Count + Polygons.Count);
            temp.AddRange(Points);
            temp.AddRange(LineStrings);
            temp.AddRange(Polygons);
            return temp;
        }
    }

    /// <summary>
    /// 点注记列表
    /// </summary>
    [SugarColumn(IsIgnore = true)]
    public List<Point> Points { get; set; } = new List<Point>();

    /// <summary>
    /// 线注记列表
    /// </summary>
    [SugarColumn(IsIgnore = true)]
    public List<LineString> LineStrings { get; set; } = new List<LineString>();

    /// <summary>
    /// 多边形注记列表
    /// </summary>
    [SugarColumn(IsIgnore = true)]
    public List<Polygon> Polygons { get; set; } = new List<Polygon>();

    /// <summary>
    /// 添加注记
    /// </summary>
    /// <param name="annotation">要添加的注记</param>
    public void AddAnnotation(Geometry annotation)
    {
        var temp1 = annotation as Point;
        if (temp1 is not null)
        {
            Points.Add(temp1);
            return;
        }

        var temp2 = annotation as LineString;
        if (temp2 is not null)
        {
            LineStrings.Add(temp2);
            return;
        }

        var temp3 = annotation as Polygon;
        if (temp3 is not null)
        {
            Polygons.Add(temp3);
            return;
        }
    }

    public void AddAnnotations(IEnumerable<Geometry> geometries) => geometries.ToList().ForEach((item) => { AddAnnotation(item); });

    public override string ToString() => JsonConvert.SerializeObject(this);
}
